package csu.web.credit_bank;

import csu.web.credit_bank.intercepter.JwtInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

@EnableAsync
@MapperScan("csu.web.credit_bank.mapper")
@SpringBootApplication
public class CreditBankApplication implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;
    public static void main(String[] args) {
        SpringApplication.run(CreditBankApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 匹配原路径结构
        String projectRoot = new ApplicationHome(getClass()).getDir().getParentFile().getParent();
        String imagePath = "file:" + projectRoot
                + "/src/main/resources/static/images/";
        registry.addResourceHandler("/images/**")
                .addResourceLocations(imagePath);
    }


    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/token",                // 登录获取token
                        "/users",                // 用户注册
                        "/users/email/**",       // 获取邮箱验证码
                        "/users/forgot-password",// 忘记密码
                        "/users/reset-password", // 重置密码
                        "/images/**",
                        "/users/**",             // 避免出问题，也添加一下
                        "/shop_items",
                        "/api/alipay/**",
                        "/api/ai/**"
                );
    }
}
